package com.wyp168.serviceedu.controller.front;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyp168.commonutils.JwtUtils;
import com.wyp168.commonutils.R;
import com.wyp168.commonutils.ordervo.CourseWebFrontVo;
import com.wyp168.serviceedu.client.OrderClient;
import com.wyp168.serviceedu.entity.EduCourse;
import com.wyp168.serviceedu.entity.chapter.ChapterVo;
import com.wyp168.serviceedu.entity.frontVo.CourseFrontVo;
import com.wyp168.serviceedu.entity.frontVo.CourseWebVo;
import com.wyp168.serviceedu.service.EduChapterService;
import com.wyp168.serviceedu.service.EduCourseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("serviceedu/coursefront")
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    //条件查询带分页课程信息
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo){
        Page<EduCourse> pageCourse = new Page<>(page,limit);
        Map<String,Object> map = courseService.getCourseFrontList(pageCourse,courseFrontVo);

        System.out.println(map);

        return R.ok().data(map);
    }


    //1.查询课程详情信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId, HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        //根据课程id查询相应课程的基本信息
        CourseWebVo courseWebVo = courseService.getCourseInfo(courseId);


        //根据课程id查询章节和小结信息
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);

        //根据课程id和用户id查询当前课程是否已经支付过
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (!StringUtils.isEmpty(memberId)) {
            boolean buyCourse = orderClient.isBuyCourse(courseId, memberId);

            return R.ok().data("courseWebVo", courseWebVo).data("chapterVideoList", chapterVideoList).data("isBuy", buyCourse);
        } else {
            return R.error().message("用户暂未登录");
        }
    }

    //根据课程id查询课程信息
    @GetMapping("getCourseInfoOrder/{courseId}")
    public CourseWebFrontVo getCourseInfoOrder(@PathVariable String courseId) {

        CourseWebVo courseInfo = courseService.getCourseInfo(courseId);
        CourseWebFrontVo courseWebFrontVo = new CourseWebFrontVo();
        BeanUtils.copyProperties(courseInfo,courseWebFrontVo);
        System.out.println("______++++++++" + courseWebFrontVo);
        return courseWebFrontVo;
    }
}
